package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {

    @Autowired
    private CourseTeacherMapper courseTeacherMapper;

    @Autowired
    private CourseBaseMapper courseBaseMapper;

    /**
     * 查询教师
     * @param courseId
     * @return
     */
    @Override
    public List<CourseTeacher> getCourseTeacher(Long courseId) {

        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseId);
        return courseTeacherMapper.selectList(queryWrapper);
    }

    /**
     * 添加/修改教师
     * @param courseTeacher
     * @return
     */
    @Override
    public CourseTeacher setCourseTeacher(Long companyId, CourseTeacher courseTeacher) {
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseBase::getId, courseTeacher.getCourseId());
        CourseBase courseBase = courseBaseMapper.selectOne(queryWrapper);
        if(!Objects.equals(courseBase.getCompanyId(), companyId)){
            XueChengPlusException.cast("只能向本机构下课程操作！");
        }


        //判断是添加还是修改
        if (courseTeacher.getId() == null) {
            //添加
            courseTeacher.setCreateDate(LocalDateTime.now());
            courseTeacherMapper.insert(courseTeacher);
            return courseTeacherMapper.selectById(courseTeacher);
        }else{
            //修改
            CourseTeacher Teacher = courseTeacherMapper.selectById(courseTeacher.getId());
            BeanUtils.copyProperties(courseTeacher,Teacher);
            courseTeacherMapper.updateById(Teacher);
            return Teacher;
        }
    }

    /**
     * 删除教师
     * @param courseId
     * @param id
     */
    @Override
    public void deleteCourseTeacher(Long companyId, Long courseId, Long id) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseId).eq(CourseTeacher::getId, id);
        CourseTeacher courseTeacher = courseTeacherMapper.selectOne(queryWrapper);
        if(courseTeacher == null){
            XueChengPlusException.cast("教师不存在！");
        }

        LambdaQueryWrapper<CourseBase> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(CourseBase::getId, courseTeacher.getCourseId());
        CourseBase courseBase = courseBaseMapper.selectOne(queryWrapper1);
        if(!Objects.equals(courseBase.getCompanyId(), companyId)){
            XueChengPlusException.cast("只能向本机构下课程操作！");
        }

        courseTeacherMapper.deleteById(courseTeacher);
    }
}
